/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.dialog.alert;

import com.bytedance.scene.dialog.ResourceTable;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.lang.ref.WeakReference;

public class AlertController {
    private Text mTitleView;
    private Text mMessageView;
    private Button mButtonPositive;
    private Button mButtonNegative;
    private Button mButtonNeutral;

    /**
     * 构造函数
     *
     * @param context context
     * @param di  Scene
     * @param window window
     */
    public AlertController(Context context, AlertDialogScene di, Window window) {
    }

    /**
     * ButtonHandler
     *
     * @since 2021-04-06
     */
    public static final class ButtonHandler extends EventHandler {
        private static final int MSG_DISMISS_DIALOG = 1;

        /**
         * ButtonHandler
         *
         * @param runner runner
         * @param dialog dialog
         */
        public ButtonHandler(EventRunner runner, AlertDialogScene dialog) {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;
            switch (eventId) { //todo 三个按钮的点击事件
                case MSG_DISMISS_DIALOG:
                    Object object = event.object;
            }
        }
    }

    /**
     * 构造
     *
     * @param context
     */
    public void AlertParams(Context context) {
    }

    /**
     * installContent
     *
     * @return int
     */
    public int installContent() {
        return selectContentView();
    }

    /**
     * 返回布局
     *
     * @return 布局ID
     */
    public int selectContentView() {
        return ResourceTable.Layout_alert_dialog;
    }

    public void setTitle(CharSequence title) {
        if (mTitleView != null) {
            mTitleView.setText(title.toString());
        }
    }

    /**
     * setCustomTitle
     *
     * @param customTitleView Component
     */
    public void setCustomTitle(Component customTitleView) {
    }

    /**
     * setMessage
     *
     * @param message CharSequence
     */
    public void setMessage(CharSequence message) {
        if (mMessageView != null) {
            mMessageView.setText(message.toString());
        }
    }

    /**
     * setView
     *
     * @param layoutResId int
     */
    public void setView(int layoutResId) {
    }
    /**
     * setView
     *
     * @param view Component
     */
    public void setView(Component view) {
    }


    /**
     * setButtonPanelLayoutHint
     *
     * @param layoutHint int
     */
    void setButtonPanelLayoutHint(int layoutHint) {
//        mButtonPanelLayoutHint = layoutHint;
    }

    /**
     * 设置button
     *
     * @param whichButton int
     * @param text text
     * @param listener listener
     * @param msg msg
     * @param cannelDialog cannelDialog
     */
    void setButton(int whichButton, CharSequence text, DialogInterface listener, InnerEvent msg,CannelDialog cannelDialog) {
        if (whichButton == ResourceTable.Id_neutral) {
            if (mButtonNeutral != null && cannelDialog!=null) {
                mButtonNeutral.setText(text.toString());
                mButtonNeutral.setClickedListener(component -> {
                    listener.onClickListen(mButtonNeutral, DialogInterface.BUTTON_NEUTRAL);
                    cannelDialog.dissmissDialog();
                });
            }
        } else if (whichButton == ResourceTable.Id_negative) {
            if (mButtonNegative != null) {
                mButtonNegative.setText(text.toString());
                mButtonNegative.setClickedListener(component -> {
                    listener.onClickListen(mButtonNegative, DialogInterface.BUTTON_NEGATIVE);
                    cannelDialog.dissmissDialog();
                });
            }
        } else if (whichButton == ResourceTable.Id_postive) {
            if (mButtonPositive != null) {
                mButtonPositive.setText(text.toString());
                mButtonPositive.setClickedListener(component -> {
                    listener.onClickListen(mButtonPositive, DialogInterface.BUTTON_POSITIVE);
                    cannelDialog.dissmissDialog();
                });
            }
        }
    }

    /**
     * resolvePanel
     *
     * @param customPanel Component
     * @param defaultPanel Component
     * @return ComponentContainer
     */
    private ComponentContainer resolvePanel(Component customPanel, Component defaultPanel) {
        return null;
    }

    /**
     * setupView
     *
     * @param contentView Component
     */
    void setupView(Component contentView) {
//        parentPanel = (DirectionalLayout) contentView.findComponentById(ResourceTable.Id_parentPanel);
        mTitleView = (Text) contentView.findComponentById(ResourceTable.Id_title);
        mMessageView = (Text) contentView.findComponentById(ResourceTable.Id_content);
        mButtonNeutral = (Button) contentView.findComponentById(ResourceTable.Id_neutral);
        mButtonNegative = (Button) contentView.findComponentById(ResourceTable.Id_negative);
        mButtonPositive = (Button) contentView.findComponentById(ResourceTable.Id_postive);
    }

    /**
     * AlertParams
     *
     * @since 2021-04-06
     */
    public static class AlertParams {
        public boolean mViewSpacingSpecified = false;
        Context mContext;
        boolean mCancelable;
        CharSequence mTitle;
        CharSequence mMessage;
        CharSequence mPositiveButtonText;
        CharSequence mNegativeButtonText;
        CharSequence mNeutralButtonText;
        DialogInterface mPositiveButtonListener;
        DialogInterface mNegativeButtonListener;
        DialogInterface mNeutralButtonListener;
        Component mView;
        int mViewLayoutResId;

        /**
         * 构造函数
         *
         * @param context context
         */
        AlertParams(Context context) {
            mContext = context;
            mCancelable = true;
        }

        /**
         * 应用dialog
         *
         * @param dialog dialog
         * @param cannelDialog cannelDialog
         */
        public void apply(AlertController dialog, CannelDialog cannelDialog) {
            if (mTitle != null) {
                dialog.setTitle(mTitle);
            }
            if (mMessage != null) {
                dialog.setMessage(mMessage);
            }
            if (mPositiveButtonText != null) {
                dialog.setButton(ResourceTable.Id_postive, mPositiveButtonText,
                        mPositiveButtonListener, null,cannelDialog);
            }
            if (mNegativeButtonText != null) {
                dialog.setButton(ResourceTable.Id_negative, mNegativeButtonText,
                        mNegativeButtonListener, null,cannelDialog);
            }
            if (mNeutralButtonText != null) {
                dialog.setButton(ResourceTable.Id_neutral, mNeutralButtonText,
                        mNeutralButtonListener, null,cannelDialog);
            }
            if (mView != null) {
                dialog.setView(mView);
            } else if (mViewLayoutResId != 0) {
                dialog.setView(mViewLayoutResId);
            }
        }
    }
}
